const express = require('express');
const proxy = require('http-proxy-middleware')
const path = require('path');

const app = express();
const port = process.env.PORT || 5000;

app.use(express.static(path.resolve(__dirname, 'dist')));

const routingRules = {
  '/app-one': 'http://localhost:5001',
  '/app-two': 'http://localhost:5002',
  '/mock': 'http://localhost:5050'
};

Object.keys(routingRules).forEach((ruleKey) => {
  app.use(ruleKey, proxy({
    target: routingRules[ruleKey],
    pathRewrite: (pathName) => {
      if (pathName.includes('app-')) {
        console.log(pathName);
        return path.join('/', pathName.split('/').slice(2).join('/'));
      }
      return pathName;
    } }));
})

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
